package com.xywang.filter;

import com.xywang.pojo.User;
import com.xywang.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthorityManagement implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
       // 权限过滤，只有管理员才能添加和删除用户
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        String method = req.getParameter("method");
        if(method==null){
            method="";
        }
        if (!(method.equals("add") || method.equals("deluser") || method.equals("modifyexe"))) {
            filterChain.doFilter(servletRequest,servletResponse);
        }else if(user.getUserRole()==1){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            System.out.println("有非管理员用户：【"+user.getUserName()+"】试图添加、修改或删除用户！");
            resp.sendRedirect(req.getContextPath()+"/error.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
